<template>
  <div v-show="editShow">
    <div class="container">
      <el-row :gutter="20">
        <div class="clearfix">
          <div class="crumbs">
            <i class="el-icon-back"></i>
            <el-button type="text" @click="goBack">返回</el-button>
          </div>
          <div class="btn-save">
            <el-button type="primary" @click="saveClick('ruleForm')">保存</el-button>
          </div>
        </div>
        <div class="form-box">
          <el-form
            :model="ruleForm"
            :rules="rules"
            ref="ruleForm"
            label-width="120px"
            class="eidt-el-form"
          >
            <el-form-item label="标题" prop="title">
              <el-input v-model="ruleForm.title"></el-input>
            </el-form-item>
            <el-form-item label="索引" prop="indexNo">
              <el-input v-model="ruleForm.indexNo" type="number"></el-input>
            </el-form-item>
            <el-form-item label="禁用/启用">
              <el-radio-group v-model="ruleForm.disabled" @change="getChageRadio">
                <el-radio :label="0">禁用</el-radio>
                <el-radio :label="1">启用</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="内容类型">
              <el-select v-model="ruleForm.projectContentTypeId" placeholder="选择内容类型">
                <el-option
                  v-for="(type,index) in projectContentType"
                  :label="type.name"
                  :key="index"
                  :value="type.projectContentTypeId"
                >
                  <div @click="getChangeContentType(type)">{{type.name}}</div>
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="内容" prop="content">
              <el-input v-model="ruleForm.content" type="textarea" rows="3"></el-input>
            </el-form-item>
          </el-form>
        </div>
      </el-row>
    </div>
  </div>
</template>

<script>

export default {
  name: "editvihicel",
  data() {
    return {
      rules: {
        title: [{ required: true, message: "标题不能为空", trigger: "blur" }],
        indexNo: [{ required: true, message: "索引不能为空", trigger: "blur" }]
      },
      ruleForm: {
        title: "",
        indexNo: "",
        disabled: 0,
        content: "",
        projectContentTypeId: "",
        projectId: localStorage.getItem("project_Id")
      },
      projectContent_Id: "",
      projectContentType: []
    };
  },
  props: {
    editShow: {
      type: Boolean,
      default: false
    },
    editData: {
      type: Object
    }
  },
  created() { 
    this.getprojectContentType();
  },
  watch: {
    editData: function() {
      const item = this.editData;
      this.ruleForm = {
        title: item.title,
        indexNo: item.indexNo,
        content: item.content,
        projectContentTypeId: item.projectContentTypeId,
        disabled:item.disabled
      };
      this.projectContent_Id = item.projectContentId;
    }
  },
  methods: {
    saveClick(ruleForm) {
      this.ruleForm = {
        title: this.ruleForm.title,
        indexNo: this.ruleForm.indexNo,
        content: this.ruleForm.content,
        disabled: this.ruleForm.disabled,
        projectContentTypeId: this.ruleForm.projectContentTypeId,
        projectId: this.projectId
      };
      this.$refs[ruleForm].validate(valid => {
        if (valid) {
          if (
            this.projectContent_Id == "" ||
            this.projectContent_Id == undefined
          ) {
            this.$axios.post("/ProjectContent/", this.ruleForm).then(res => {
              if (res.data.hasError == false) {
                this.$message.success("保存成功！");
                this.goBack();
                this.$refs[ruleForm].resetFields();
              } else {
                this.$message.error("保存失败！");
              }
            });
          } else {
            this.$axios
              .put(
                "/ProjectContent/" + this.projectContent_Id + "",
                this.ruleForm
              )
              .then(res => {
                if (res.data.hasError == false) {
                  this.$message.success("编辑成功！");
                  this.goBack();
                  this.$refs[ruleForm].resetFields();
                } else {
                  this.$message.error("编辑失败！");
                }
              });
          }
        } else {
          return false;
        }
      });
    },
    goBack() {
      this.$emit("backFunStu");
      this.projectContent_Id = "";
    },
    getChageRadio(value) {
      this.ruleForm.disabled = value;
    },
    getprojectContentType() {
      this.$axios.get("/ProjectContentType/").then(res => {
        if (res.data.hasError == false) {
          this.projectContentType = [...res.data.data];
        }
      });
    },
    getChangeContentType(type) {
      this.ruleForm.projectContentTypeId = type.projectContentTypeId;
    }
  }
};
</script>